<?php
namespace app\controller;

use app\BaseController;
use liliuwei\think\Auth;
use think\facade\Db;
use think\facade\Session;

class Admin extends BaseController
{
    protected $gobaluser=0;
    protected $admin=0;
    public function __construct(){
        checkuser();
        $this->gobaluser=Session::get("uid");
        $this->admin=Session::get("admin");
    }
    public function home(){
        echo "home页";
    }
    public function heartRate(){
        echojson([],0,'',0);
    }
    public function index()
    {
        $user=DB::name("admin")->where("id",$this->gobaluser)->find();
        $res=DB::name("auth_group_access")->where("uid",$this->gobaluser)->select();
        $ids=[];
        if($res){
            foreach ($res as $v){
                $ids[]=$v['group_id'];
            }
        }
        $auths=DB::name("auth_group")->where("id","in",$ids)->where("status",1)->select();
        $ids_str='';
        if($auths){
            foreach ($auths as $v){
                $ids_str .="," . $v['rules'];
                $ids_str = trim($ids_str,",");
            }
        }

        $authModel = new Auth();
        $auth = $authModel->check("admin/all",$this->gobaluser);
        $menu=DB::name("auth_rule")->where("pathid",0)->where("status",1)->where("classty",2)->where("sortid",">=",0)->order("sortid desc");
        if($auth){
            $menu=$menu->select();
        }else{
            $menu=$menu->where("id","in",$ids_str)->select();
        }

        $menu=$menu->all();
        if($menu){
            foreach ($menu as $k => $v){
                $menu2=DB::name("auth_rule")->where("pathid",$v['id'])->where("status",1)->whereIn("classty",[2,3])->order("sortid desc");
                if($auth){
                    $menu2=$menu2->select();
                }else{
                    $menu2=$menu2->where("id","in",$ids_str)->select();
                }

                $menu[$k]['menu2']=$menu2;
	            $menu[$k]['menu2']= $menu[$k]['menu2']->all();
	            foreach ($menu2 as $k2 => $v2){
		            $menu3=DB::name("auth_rule")->where("pathid",$v2['id'])->where("status",1)->whereIn("classty",[2,3])->order("sortid desc");
		            if($auth){
			            $menu3=$menu3->select();
		            }else{
			            $menu3=$menu3->where("id","in",$ids_str)->select();
		            }

		            $menu[$k]['menu2'][$k2]['menu3']=$menu3;
	            }
            }
        }else{
//            $menu['menu2']=[];
        }



        return view("admin/index")->assign(["user"=>$user,'menu'=>$menu]);
    }
}
